package com.itheima;


import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.*;

public class JULTest2 {


    @Test
    public void testQuick() {

        //获取日志记录器
        Logger logger = Logger.getLogger("com.itheima.JULTest2");

        //日志记录输出
        logger.info("hello jul");

        //通用方法进行日志记录
        logger.log(Level.INFO,"info msg");

        //通过占位符方法输出变量值
        String name="itcast";
        int age=13;
        logger.log(Level.INFO,"用户信息:{0},{1}",new Object[]{name,age});

    }

    @Test
    public void testLogLevel(){
        //获取日志记录器
        Logger logger = Logger.getLogger("com.itheima.JULTest2");
        //日志记录输出

        logger.severe("severe");
        logger.warning("warning");
        logger.info("info");//默认只输出到info级别以上
        logger.config("config");
        logger.fine("fine");
        logger.finer("finer");
        logger.finest("finest");
    }

    @Test
    public void testLogConfig() throws IOException {
        //获取日志记录器对象
        Logger logger = Logger.getLogger("com.itheima.JULTest2");
        logger.setUseParentHandlers(false);

        ConsoleHandler consoleHandler=new ConsoleHandler();
        SimpleFormatter simpleFormatter=new SimpleFormatter();
        consoleHandler.setFormatter(simpleFormatter);
        consoleHandler.setLevel(Level.CONFIG);
        logger.addHandler(consoleHandler);
        logger.setLevel(Level.ALL);

        FileHandler fileHandler=new FileHandler("./jul.log");
        fileHandler.setLevel(Level.CONFIG);
        fileHandler.setFormatter(simpleFormatter);
        logger.addHandler(fileHandler);

        //日志记录输出
        logger.severe("severe");
        logger.warning("warning");
        logger.info("info");
        logger.config("config");
        logger.finer("fine");
        logger.finer("finer");
        logger.finest("finest");

    }

}
